package com.gamecauca;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;
import com.gamecauca.animal.Fish;
import com.gamecauca.animal.FishAmountLevel;
import com.gamecauca.common.Colliders;
import com.gamecauca.common.Constant;
import com.gamecauca.common.GameConfig;
import com.gamecauca.common.GameObject;
import com.gamecauca.common.Pools;
import com.gamecauca.externalfactor.Cloud;
import com.gamecauca.item.Bomb;
import com.gamecauca.item.ItemsFunctions;
import com.gamecauca.screen.GameCauCa;
import com.gamecauca.screen.GameScreen;


public class World {
	private final Pool<Fish> fishPool;
	private final Pool<Cloud> cloudPool;
	private final Pool<Bomb> bombPool;
	public Player player;
	public  final int INIT_FISH = 5;
	public  final int MAX_FISH = 100;
	private  final int INIT_CLOUD = 2;
	private  final int MAX_CLOUD = 10;
	private  final int INIT_BOMB = 2;
	private  final int MAX_BOMB = 100;
	private Array<Fish> arrFishs;
	private Array<Cloud> arrCloud;
	private Array<Bomb> arrBombs;
	private int numFishs;
	private int numClouds;
	private int level;
	private int kindFishToPassLevel;
	private static int HEIGHOFOBJECT = 50;// tru hao 1 doan object
	private static int WIGHTOFOBJECT = 50;// tru hao 1 doan object
	private int timeToCreateObject;
	private Fish tempFish = null;
	private Array<Integer> tempIndexFish = new Array<Integer>();
	private Array<Fish> tempArrFish = new Array<Fish>();
	private boolean checkFish;
	private int numFishToPassLevel;
	private List<FishAmountLevel> fishLevel;
	public static  boolean isCatchOk = false;// khi cau trung duoc 1 thu gi do,bat co nay// len
	
	public static Vector2 gravity = new Vector2(0, 2);// trong luc
//	public int xNext = 10;
	private int strengthDown;
	public int fastDown = 0;
	
	private OrthographicCamera camera;
	
	
	private ItemsFunctions functions;
	
	// public static boolean isCreate = false;
	// private Rectangle boundOfMap = new Rectangle(0-WIGHTOFOBJECT, 0 -
	// HEIGHOFOBJECT, 900, 480) ;//bien cua man hinh cac object duoc phep ton
	// tai

	public World() {
		level = 1;
		numFishs = 9;
		numClouds = 2;
		kindFishToPassLevel = 0;
		numFishToPassLevel = 3;
		strengthDown = 0;
		timeToCreateObject = 30;// sau 30 vong paint tu luc go object khoi danh
								// sach
		
		camera = new OrthographicCamera(Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		camera.position.set(Constant.SCREEN_WIDTH/ 2, Constant.SCREEN_HEIGHT / 2, 0);
		
		fishLevel = new ArrayList<FishAmountLevel>();
		
		functions = new ItemsFunctions(); 
		
		// chung ta se goi pool.obtain() de tao moi object va them vao danh sach
		fishPool = new Pool<Fish>(INIT_FISH, MAX_FISH) {
			@Override
			protected Fish newObject() {
				return new Fish();
			}
		};
		cloudPool = new Pool<Cloud>(INIT_CLOUD, MAX_CLOUD) {
			@Override
			protected Cloud newObject() {
				return new Cloud();
			}
		};
		bombPool = new Pool<Bomb>(INIT_BOMB, MAX_BOMB) {
			@Override
			protected Bomb newObject() {
				int r = GameCauCa.rand.nextInt(5)+16;
				return new Bomb(r);
			}
		};
		player = new Player(0);
		Initialize(level);

	}

	// khoi tao cac gia tri dau tien cho World
	public void Initialize(int level) {
		
		player.lineLengthInLevel = 200;
		int[] kind = new int[numFishs];
		int countKind = 0;
		
		
		// khoi tao danh sach ca,gan danh sach vao Pool
		arrFishs = Pools.makeArrayFromPool(arrFishs, fishPool, MAX_FISH);
		arrCloud = Pools.makeArrayFromPool(arrCloud, cloudPool, MAX_CLOUD);
		arrBombs = Pools.makeArrayFromPool(arrBombs, bombPool, MAX_BOMB);
		
		if (level == 1) {
			// những gi riêng của level 1
			countKind = 2;
			kind[0] = 0;
			kind[1] = 1;
			
			fishLevel.add(new FishAmountLevel(0, 5));
			
			
		} else if (level == 2) {		
			// những gi riêng của level 1
			countKind = 3;
			kind[0] = 0;
			kind[1] = 1;
			kind[2] = 2;
			
			fishLevel.clear();	
			fishLevel.add(new FishAmountLevel(1, 10));
			
			
		} else if (level == 3) {
			// những gi riêng của level 1
			countKind = 5;
			kind[0] = 1;
			kind[1] = 2;
			kind[2] = 3;
			kind[3] = 15;
			kind[4] = 16;
			
			fishLevel.clear();	
			fishLevel.add(new FishAmountLevel(2, 5));
			fishLevel.add(new FishAmountLevel(3, 10));
			
		} else if (level == 4) {
			
			countKind = 5;
			kind[0] = 2;
			kind[1] = 3;
			kind[2] = 4;
			kind[3] = 15;
			kind[4] = 16;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(4, 12));
			
			
		} else if (level == 5) {
			
			countKind = 5;
			kind[0] = 3;
			kind[1] = 4;
			kind[2] = 5;
			kind[3] = 15;
			kind[4] = 16;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(4, 5));
			fishLevel.add(new FishAmountLevel(5, 7));
			
		} else if (level == 6) {
			
			countKind = 5;
			kind[0] = 4;
			kind[1] = 5;
			kind[2] = 6;
			kind[3] = 17;
			kind[4] = 18;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(6, 10));
			
		} else if (level == 7) {
			
			countKind = 5;
			kind[0] = 5;
			kind[1] = 6;
			kind[2] = 7;
			kind[3] = 17;
			kind[4] = 18;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(6, 5));
			fishLevel.add(new FishAmountLevel(7, 7));
			
		} else if (level == 8) {

			countKind = 5;
			kind[0] = 6;
			kind[1] = 7;
			kind[2] = 8;
			kind[3] = 17;
			kind[4] = 18;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(8, 15));
			
		} else if (level == 9) {
			
			countKind = 5;
			kind[0] = 7;
			kind[1] = 8;
			kind[2] = 9;
			kind[3] = 17;
			kind[4] = 18;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(8, 4));
			fishLevel.add(new FishAmountLevel(9, 8));

		} else if (level == 10) {
			
			countKind = 5;
			kind[0] = 8;
			kind[1] = 9;
			kind[2] = 10;
			kind[3] = 17;
			kind[4] = 18;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(9, 5));
			fishLevel.add(new FishAmountLevel(10, 7));
			

		} else if (level == 11) {
			
			countKind = 5;
			kind[0] = 9;
			kind[1] = 10;
			kind[2] = 11;
			kind[3] = 18;
			kind[4] = 19;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(11, 10));

		} else if (level == 12) {
			
			countKind = 5;
			kind[0] = 10;
			kind[1] = 11;
			kind[2] = 12;
			kind[3] = 17;
			kind[4] = 19;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(12, 15));

		} else if (level == 13) {
			
			countKind = 5;
			kind[0] = 11;
			kind[1] = 12;
			kind[2] = 13;
			kind[3] = 18;
			kind[4] = 19;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(13, 10));

		} else if (level == 14) {
			
			countKind = 5;
			kind[0] = 12;
			kind[1] = 13;
			kind[2] = 14;
			kind[3] = 16;
			kind[4] = 18;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(13, 5));
			fishLevel.add(new FishAmountLevel(14, 10));

		} else if (level == 15) {
			
			countKind = 6;
			kind[0] = 11;
			kind[1] = 12;
			kind[2] = 13;
			kind[3] = 14;
			kind[4] = 18;
			kind[5] = 19;
			
			fishLevel.clear();
			fishLevel.add(new FishAmountLevel(13, 10));
			fishLevel.add(new FishAmountLevel(14, 10));
		}
		
		
		for (int i = 0; i < numFishs; i++) {
			int sideapp = GameCauCa.rand.nextInt(2);
			// tao random huong xuat
			// hien cho ca,co the the la 0,hoac 1
			int y_Appear = GameCauCa.rand.nextInt(180);// tao random toa do Y
														// xuat hien sao cho ca
														// nam phia duoi nuoc :
														// toa do nuoc tu
														// 0--->240
			int kindFish = kind[GameCauCa.rand.nextInt(countKind)];
			if (sideapp == Constant.LEFTSIDE) {
				Fish fish = new Fish(0, y_Appear, Assets.fishRegionWidth[kindFish], Assets.fishRegionHeight[kindFish], kindFish,
						Constant.LEFTSIDE);
				arrFishs.add(fish);
			} else if (sideapp == Constant.RIGHTSIDE) {
				Fish fish = new Fish(820, y_Appear, Assets.fishRegionWidth[kindFish], Assets.fishRegionHeight[kindFish], kindFish,
						Constant.RIGHTSIDE);
				arrFishs.add(fish);
			}
			// System.out.println("X: "+arrFishs.get(i).position.x+"  Y:  "+arrFishs.get(i).position.y+"Wight: "+arrFishs.get(i).bounds.getWidth()+" Height: "+arrFishs.get(i).bounds.getHeight());
		}
		
		// khoi tao danh sach muc,may, gan danh sach vao pool
		for (int i = 0; i < numClouds; i++) {
			int sideapp = GameCauCa.rand.nextInt(2);// tao random huong xuat
													// hien cho ca,co the the la
													// 0,hoac 1
			int y_Appear = GameCauCa.rand.nextInt(50)+250;// tao random toa do Y
														// xuat hien sao cho ca
														// nam phia duoi nuoc :
														// toa do nuoc tu
														// 0--->240
			int kindCloud = GameCauCa.rand.nextInt(3);
			if (sideapp == Constant.LEFTSIDE) {
				Cloud cloud = new Cloud(kindCloud, 700, y_Appear, 3, 0, Constant.RIGHT, Constant.LEFT);
				arrCloud.add(cloud);
			} else if (sideapp == Constant.RIGHTSIDE) {
				Cloud cloud = new Cloud(kindCloud, 100, y_Appear, 3, 0, Constant.LEFT, Constant.RIGHT);
				arrCloud.add(cloud);
			}
		}
	}

	public void render(SpriteBatch batch) {
		
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		
		batch.draw(Assets.backgroundRegion,0, 0, Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		batch.draw(Assets.backgroundRegion,800, 0, Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		batch.draw(Assets.backgroundRegion,1600, 0, Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		batch.draw(Assets.backgroundRegion,2400, 0, Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		batch.draw(Assets.backgroundRegion,3200, 0, Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		batch.draw(Assets.backgroundRegion,4000, 0, Constant.SCREEN_WIDTH,Constant.SCREEN_HEIGHT);
		
		if (player != null)
			player.render(batch);
		if (arrFishs != null)
			render(arrFishs, batch);
		if (arrCloud != null)
			render(arrCloud, batch);
		if (arrBombs != null)
			render(arrBombs, batch);
	}

	public void update(Input input) {
		// System.out.println(" ============ arrFishs.size:              "+arrFishs.size);
		checkCreateObject();// sau 20 vong paint check tao them doi tuong
		timeToCreateObject++;
		if (timeToCreateObject > 1000000)
			timeToCreateObject = 0;
		if (player != null)
			player.update(input);
		checkOutOfBounds();// goi ham check bien truoc khi goi ham update
		update(arrFishs);// update danh sach ca
		// update danh sach muc
		// update danh sach tom,item
		
		update(arrCloud);// update danh sach ca
		// if(timeToCreateObject %5 == 0)
		// update danh sach bomb
		//System.out.println("Size: " + arrBombs.size);
		update(arrBombs);
	}

	private void checkCreateObject() {
		// check neu so luong ca nho hon so luong cua no thi them vao
		if (arrFishs.size < numFishs) {
			// System.out.println(" ============ arrFishs.size:              "+arrFishs.size
			// +"numFishs: "+numFishs);
			int temp = arrFishs.size;
			int[] kind = new int[numFishs];
			int countKind = 0;
			
			if (level == 1) {
				// những gi riêng của level 1
				countKind = 2;
				kind[0] = 0;
				kind[1] = 1;
				
			} else if (level == 2) {		
				// những gi riêng của level 1
				countKind = 3;
				kind[0] = 0;
				kind[1] = 1;
				kind[2] = 2;
				
			} else if (level == 3) {
				// những gi riêng của level 1
				countKind = 5;
				kind[0] = 1;
				kind[1] = 2;
				kind[2] = 3;
				kind[3] = 15;
				kind[4] = 16;
				
			} else if (level == 4) {
				
				countKind = 5;
				kind[0] = 2;
				kind[1] = 3;
				kind[2] = 4;
				kind[3] = 15;
				kind[4] = 16;
				
			} else if (level == 5) {
				
				countKind = 5;
				kind[0] = 3;
				kind[1] = 4;
				kind[2] = 5;
				kind[3] = 15;
				kind[4] = 16;
				
			} else if (level == 6) {
				
				countKind = 5;
				kind[0] = 4;
				kind[1] = 5;
				kind[2] = 6;
				kind[3] = 17;
				kind[4] = 18;
				
			} else if (level == 7) {
				
				countKind = 5;
				kind[0] = 5;
				kind[1] = 6;
				kind[2] = 7;
				kind[3] = 17;
				kind[4] = 18;
				
			} else if (level == 8) {

				countKind = 5;
				kind[0] = 6;
				kind[1] = 7;
				kind[2] = 8;
				kind[3] = 17;
				kind[4] = 18;
				
			} else if (level == 9) {
				
				countKind = 5;
				kind[0] = 7;
				kind[1] = 8;
				kind[2] = 9;
				kind[3] = 17;
				kind[4] = 18;

			} else if (level == 10) {
				
				countKind = 5;
				kind[0] = 8;
				kind[1] = 9;
				kind[2] = 10;
				kind[3] = 17;
				kind[4] = 18;

			} else if (level == 11) {
				
				countKind = 5;
				kind[0] = 9;
				kind[1] = 10;
				kind[2] = 11;
				kind[3] = 18;
				kind[4] = 19;

			} else if (level == 12) {
				
				countKind = 5;
				kind[0] = 10;
				kind[1] = 11;
				kind[2] = 12;
				kind[3] = 17;
				kind[4] = 19;

			} else if (level == 13) {
				
				countKind = 5;
				kind[0] = 11;
				kind[1] = 12;
				kind[2] = 13;
				kind[3] = 18;
				kind[4] = 19;

			} else if (level == 14) {
				
				countKind = 5;
				kind[0] = 11;
				kind[1] = 12;
				kind[2] = 13;
				kind[3] = 16;
				kind[4] = 18;

			} else if (level == 15) {
				
				countKind = 6;
				kind[0] = 11;
				kind[1] = 12;
				kind[2] = 13;
				kind[3] = 14;
				kind[4] = 18;
				kind[5] = 19;
			}
			
			for (int i = temp; i <= numFishs; i++) {
				// System.out.println(" ============i:    "+i);
				// System.out.println(" ============size:    "+arrFishs.size);
				Fish fish = fishPool.obtain();
				int y_Appear = GameCauCa.rand.nextInt(180);// tao random toa do
															// Y xuat hien sao
															// cho ca nam phia
															// duoi nuoc : toa
															// do nuoc tu
															// 0--->240
				int kindFish = kind[GameCauCa.rand.nextInt(countKind)];// random lai kind de set
														// vao fish
				int x_speed = GameCauCa.rand.nextInt(5) + 1;
//				fish.isCapture = false;
				fish.setKind(kindFish);
				fish.setVxy(x_speed, 0);
				fish.bounds.setWidth(Assets.fishRegionWidth[kindFish]);
				fish.bounds.setHeight(Assets.fishRegionHeight[kindFish]);
				// nen randdom them van toc cua ca
				if (fish.direction == Constant.LEFT
						|| fish.direction == Constant.LEFT_DOWN
						|| fish.direction == Constant.LEFT_UP
						|| fish.direction == Constant.UP) {
					fish.initFish(kindFish, Constant.RIGHTSIDE);
					fish.position.set(820, y_Appear);
				} 
				else if (fish.direction == Constant.RIGHT
						|| fish.direction == Constant.RIGHT_DOWN
						|| fish.direction == Constant.RIGHT_UP) {
					fish.initFish(kindFish, Constant.LEFTSIDE);
					fish.position.set(0, y_Appear);
					
				} 
				arrFishs.add(fish);
			}
		}
		// check neu so luong tom,muc ,rong reu nho hon so luong quy dinh thi
		// them vao
	}

	private void update(Array<? extends GameObject> gos) {
		for (GameObject go : gos) {
			go.update();
		}
	}

	private void render(Array<? extends GameObject> gos, SpriteBatch batch) {
		for (GameObject go : gos) {
			go.render(batch);
		}
	}

	// check va cham cua tat ca cac object trong game voi bien
	private void checkOutOfBounds() {
		checkFishOutOfBounds(arrFishs); // check va cham cua fish voi bien
		// check va cham cua item voi bien
		// check va cham cua tom muc voi bien
	}

	private void checkFishOutOfBounds(Array<Fish> arrFishs2) {
		// TODO Auto-generated method stub
		// System.out.println("LocNNT :   "+arrFishs.size);
		Colliders.removeOutOfBounds(fishPool, arrFishs);// check xem co thang
														// nao ra khoi bien ko
		// neu co go bo object thi bat co isCreate = true
	}
	
	private boolean checkBombOutOfBounds(Bomb bomb, int index)
	{
		if(bomb.isBombFailed()){
			
			try{
				Colliders.removeOutOfBombBounds(bombPool, arrBombs, bomb, index);
			}catch(Exception ex){
				return false;
			}
			
			fastDown = 0;
			strengthDown = 0;
			return true;
		}
		
		return false;
	}

	class Player extends GameObject {
		public float speed = 2;
		private float lineSpeed = 5;
		private int statePlayer;
		double floatingLevel; // muc do troi noi tren nuoc, duoc hieu nhu la  speed
		private float stateTime ;
		public float speedThrowBar;
		public float x_fishHook; // day cau
		public float y_fishHook;
		public float x_throwBomb;
		public float y_throwBomb;
		
		public int xFishing;// toa do X buong cau
		public int yFishing;// toa do X buong cau
		public int direction;
		public int sideAppear;
		public int blood;// mau nguoi cau ca
		public int strength;// suc khoe nguoi cau ca,neu co suc khoe lon thi co the cau duoc ca lon
		public int lengthsOfline;// neu day cau dai thi co the cau ca o duoi
		public int amountOfMoney;
		public int countShootTime = 1;
		public int overFlowFireBombX;
		public int overFlowFireBombY;
		public int countFireBomb;
		public int speedThrowBomb;
		public int oldLineLength; // day cau ban dau
		public int lineLength; // day cau
		public int lineLengthInLevel; // day cau
		public int bombIndex;
		public int bombId;
		private float iTimer;
		private int iCounter = Constant.BANDAU;		
		private float xMoney = 0, yMoney = 0;
		private float xCollectFish0= 0, xCollectFish1 = 0, yCollectFish = 0;
		
		
		private TextureRegion currentBoatFrame ; 
		private TextureRegion currentPlayerFrame ; 
		private TextureRegion currentDogFrame ; 
		private Animation boatGoLeft;
		private Animation boatGoRight;
		private Animation playerGoLeft;
		private Animation playerGoRight;
		private Animation playerThacau_left;
		private Animation playerThacau_right;
		private Animation dogDangranh_left;
		private Animation dogDangranh_right;
		private Animation dogVotca_left;
		private Animation dogVotca_right;
		
		public boolean overFlowFire = false;
		public boolean pushDown;
		public boolean isShoot = false;
		public boolean isMoving;// khi con thuyen dang di chuyen
		public boolean isTimeDelayInFishing;
		public boolean isFishing;// bat len khi nguoi chay den vi tri buong  cau,hoac buong cau
		public boolean isFinished = false;
		public boolean isCollect = false;//check xem bat dau vot ca
		public boolean stillBombThrow = false;
		
		
		public Player(int sideAppear) {
			this.position = new Vector2(300, Constant.WATERLINE);
			if (this.sideAppear == Constant.LEFTSIDE) {
				direction = Constant.LEFT;
			} else if (sideAppear == Constant.RIGHTSIDE) {
				direction = Constant.RIGHT;
			}
			initPlayer(sideAppear);
		}

		public Player(int x, int y, int direction, int sideAppear, int blood,
				int strength, int lengthsOfline, int amoutnOFMoney) {
			super(x, y, Assets.staticboatGoLeft[0].getRegionWidth(), Assets.staticboatGoLeft[0].getRegionHeight());
			this.direction = direction;
			initPlayer(sideAppear);
		}
		public void initPlayer(int sideAppear){
//			Log.e("locnnt", "gia tri cua banh mi la:  "+GameConfig.getPrice(Constant.BANHMI));
			this.sideAppear = sideAppear;
			blood = GameConfig.getPlayerBood();
			strength = GameConfig.getPlayerStreng();
			lengthsOfline = GameConfig.getlengOfLine();
			this.kindOfObject = Constant.PERSON;
			amountOfMoney = GameConfig.getPlayerMoney() ;
//			isCatchOk = false;
			isTimeDelayInFishing = false;
			isMoving = false;
			statePlayer = Constant.STANDBY;
			floatingLevel = 0.2;
			lineLength = Assets.lineRegion.getRegionHeight();
			oldLineLength = lineLength;
			pushDown = false;
			lineLengthInLevel = 200;
			speedThrowBomb = 0;
			currentBoatFrame = Assets.staticboatGoLeft[0];
			currentPlayerFrame = Assets.playerRight[0];
			currentDogFrame  = Assets.leftDogfree[0];
			boatGoLeft = new Animation(0.25f, Assets.staticboatGoLeft); 
			boatGoRight = new Animation(0.25f, Assets.staticboatGoRight);
			playerGoLeft = new Animation(0.25f, Assets.playerLeft); 
			playerGoRight = new Animation(0.25f, Assets.playerRight); 
			playerThacau_left = new Animation(0.25f, Assets.playerLeftThacau); 
			playerThacau_right = new Animation(0.25f, Assets.playerRightThacau);
			dogDangranh_left= new Animation(0.25f, Assets.leftDogfree);
			dogDangranh_right= new Animation(0.25f, Assets.rightDogfree);
			dogVotca_left= new Animation(0.25f, Assets.leftDogVotca);
			dogVotca_right= new Animation(0.25f, Assets.rightDogVotca);
		}

		public void render(SpriteBatch batch) {
//			Log.e("locnnt", "state:   ==========="+statePlayer);
			stateTime += Gdx.graphics.getDeltaTime();
			
	
			float xPerson = 0,yPerson = 0;
			float xBoat= 0 ,yBoat = 0;
			float xDog = 0,yDog = 0;
			
			
			Assets.lineRegion.setRegionHeight(lineLength);
			
			if(statePlayer == Constant.GOTOLEFT ||(statePlayer == Constant.STANDBY && direction == Constant.LEFT)){
//				Log.e("locnnt", "Constant.STANDBY:   ==========="+Constant.STANDBY);
				currentBoatFrame = boatGoLeft.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				currentPlayerFrame = playerGoLeft.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				currentDogFrame = dogDangranh_left.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				xBoat = position.x;
				yBoat = position.y-60;
				xPerson = position.x+60;
				yPerson = position.y-35;
				xDog = position.x-5;
				yDog = position.y-50;
				xMoney = position.x + 20;
				yMoney = position.y + 20;
				xCollectFish0 = xBoat + 60;
				xCollectFish1 = xBoat;
				yCollectFish = yBoat + 20;
				
				x_throwBomb = xPerson - 30;
				y_throwBomb = yPerson + 20;
			}
			else if(statePlayer == Constant.GOTORIGHT || (statePlayer == Constant.STANDBY && direction == Constant.RIGHT)){
				currentBoatFrame = boatGoRight.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				currentPlayerFrame = playerGoRight.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				currentDogFrame = dogDangranh_right.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				xBoat = position.x;
				yBoat = position.y-60;
				xPerson = position.x+20;
				yPerson = position.y-35;
				xDog = position.x+85;
				yDog = position.y-50;
				xMoney = position.x + 40;
				yMoney = position.y + 20;
				xCollectFish0 = xBoat + 80;
				xCollectFish1 = xBoat + 141;
				yCollectFish = yBoat + 20;
				
				x_throwBomb = xPerson + 64;
				y_throwBomb = yPerson + 20;
			}
			else if(statePlayer == Constant.FISHING && direction == Constant.LEFT ){
				currentBoatFrame = boatGoLeft.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				currentPlayerFrame = playerThacau_left.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				x_fishHook = position.x - 19;
				y_fishHook = position.y + 15 - lineLength;
				xBoat = position.x;
				yBoat = position.y-60;
				xPerson = position.x-10;
				yPerson = position.y-35;
				xDog = position.x-5;
				yDog = position.y-50;
				xCollectFish0 = xBoat + 60;
				xCollectFish1 = xBoat;
				yCollectFish = yBoat + 20;
				x_throwBomb = xPerson - 30;
				y_throwBomb = yPerson + 20;
				
				currentDogFrame = dogDangranh_left.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				batch.draw(Assets.lineRegion, position.x-5, position.y + 35 - lineLength);
				if(!isCatchOk)batch.draw(Assets.hookRegion[0], x_fishHook, y_fishHook);
			}
			else if(statePlayer == Constant.FISHING && direction == Constant.RIGHT){
//				Log.e("LocNNT", "states ========: Constant.FISHING && direction == Constant.RIGHT "+state);
				currentBoatFrame = boatGoRight.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				currentPlayerFrame = playerThacau_right.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				currentDogFrame = dogDangranh_right.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				x_fishHook = position.x + 135;
				y_fishHook = position.y + 15 - lineLength;
				xBoat = position.x;
				yBoat = position.y-60;
				xPerson = position.x;
				yPerson = position.y-35;
				xDog = position.x+85;
				yDog = position.y-50;
				xCollectFish0 = xBoat + 80;
				xCollectFish1 = xBoat + 141;
				yCollectFish = yBoat + 20;
				
				x_throwBomb = xPerson + 64;
				y_throwBomb = yPerson + 20;
				
				batch.draw(Assets.lineRegion, position.x + 150	, position.y + 30 - lineLength);
//				batch.draw(Assets.hookRegion[0], x_fishHook, y_fishHook);
				if(!isCatchOk)batch.draw(Assets.hookRegion[0], x_fishHook, y_fishHook);
			}
			else if(statePlayer == Constant.COLLECT && direction == Constant.LEFT ){
				
				xCollectFish0 = xBoat + 60;
				xCollectFish1 = xBoat;
				yCollectFish = yBoat + 20;
				
				x_throwBomb = xPerson - 30;
				y_throwBomb = yPerson + 20;
				
				iTimer += 0.035f;
				currentBoatFrame = boatGoLeft.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				currentPlayerFrame = playerThacau_left.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				
				currentDogFrame = dogVotca_left.getKeyFrame(iTimer, Animation.ANIMATION_LOOPING);
				
				if(iTimer > 1.25f){
					statePlayer = Constant.STANDBY;				
					iTimer = 0;
				}		

				x_fishHook = position.x - 19;
				y_fishHook = position.y + 15 - lineLength;
				xBoat = position.x;
				yBoat = position.y-60;
				xPerson = position.x;
				yPerson = position.y-35;
				xDog = position.x - 20;
				yDog = position.y-60;
				batch.draw(Assets.lineRegion, position.x-5, position.y + 35 - lineLength);
//				batch.draw(Assets.hookRegion[0], x_fishHook, y_fishHook);
				if(!isCatchOk)batch.draw(Assets.hookRegion[0], x_fishHook, y_fishHook);
			}
			else if(statePlayer == Constant.COLLECT && direction == Constant.RIGHT){
				
				xCollectFish0 = xBoat + 80;
				xCollectFish1 = xBoat + 141;
				yCollectFish = yBoat + 20;			
				x_throwBomb = xPerson + 64;
				y_throwBomb = yPerson + 20;
				
				iTimer += 0.035f;
				currentBoatFrame = boatGoRight.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING); 
				currentPlayerFrame = playerThacau_right.getKeyFrame(stateTime , Animation.ANIMATION_LOOPING);
				
				currentDogFrame = dogVotca_right.getKeyFrame(iTimer , Animation.ANIMATION_LOOPING);
				
				if(iTimer > 1.25f){
					statePlayer = Constant.STANDBY;
					iTimer = 0;
				}		
				
				x_fishHook = position.x + 135;
				y_fishHook = position.y + 15 - lineLength;
				xBoat = position.x;
				yBoat = position.y-60;
				xPerson = position.x;
				yPerson = position.y-35;
				xDog = position.x+55;
				yDog = position.y-60;
				batch.draw(Assets.lineRegion, position.x + 150	, position.y + 30 - lineLength);
//				batch.draw(Assets.hookRegion[0], x_fishHook, y_fishHook);
				if(!isCatchOk)batch.draw(Assets.hookRegion[0], x_fishHook, y_fishHook);
			}
			
			batch.draw(currentPlayerFrame, xPerson, yPerson);
			batch.draw(currentBoatFrame, xBoat, yBoat);
			batch.draw(currentDogFrame, xDog, yDog);
			batch.draw(Assets.timerBarRegion, position.x, position.y + 64, speedThrowBomb, Assets.timerBarRegion.getRegionHeight());
		
		}

		public void update(Input input) {
			
			checkBoatCollisionFishDeadByBomb();
			
			if (input.buttons[Input.LEFT] && statePlayer != Constant.FISHING) {
				// phai check xem da cau xong chua?
				
				direction = Constant.LEFT;
				statePlayer = Constant.GOTOLEFT;
			} 
			else if (input.buttons[Input.RIGHT] && statePlayer != Constant.FISHING) {
				
				direction = Constant.RIGHT;
				statePlayer = Constant.GOTORIGHT;
			}
			else if (input.buttons[Input.CATCH] && statePlayer != Constant.FISHING) {			
						
				if (!pushDown) {
					pushDown = true;
					statePlayer = Constant.FISHING;// neu nguoi dung chua buong cau
												// thi moi bat co len
				}
			}
			else if (input.buttons[Input.ATTACK] && statePlayer != Constant.FISHING) {
				
				input.releaseAllKeys();
				
				if(GameScreen.itemIndex >= 0){
					
					if(Assets.userBox.get(GameScreen.itemIndex).getKind().equals("Time")){
						if(!isShoot){
							statePlayer = Constant.TIME;
							isShoot = true;
						}
					}
					else if(Assets.userBox.get(GameScreen.itemIndex).getKind().equals("Bomb")){
					
						if(GameScreen.strengPercent > 0){
							
							fastDown++;
							
							if(GameScreen.itemIndex != -1){
								bombIndex = Assets.userBox.get(GameScreen.itemIndex).getIndex();
								bombId = Assets.userBox.get(GameScreen.itemIndex).getId();
								
								if(speedThrowBomb >= 100){
									speedThrowBomb = 0;
									fastDown = 0;
									strengthDown = 0;
									speedThrowBar = 0;
									
								}else{
									speedThrowBomb+= 2;
									speedThrowBar += 1;
								}
							}
						}
						else{
							speedThrowBomb = 0;
						}
						
					
					}
					else if(Assets.userBox.get(GameScreen.itemIndex).getKind().equals("Line")){
						if(!isShoot){
							statePlayer = Constant.LINE;
							isShoot = true;
						}
					}
					else if(Assets.userBox.get(GameScreen.itemIndex).getKind().equals("Strength")){
						if(!isShoot){
							statePlayer = Constant.STRENGTH;
							isShoot = true;
						}	
					}
					else if(Assets.userBox.get(GameScreen.itemIndex).getKind().equals("Health")){
						if(!isShoot){
							statePlayer = Constant.HEALTH;
							isShoot = true;
						}
					}
				}
			}
			else
			{
				isShoot = false;
				
				if(speedThrowBomb > 0){
					
					strengthDown = fastDown/2;
					strength -= strengthDown;
					fastDown = 0;
					
					addBomb(speedThrowBar, bombIndex, bombId);
					speedThrowBomb = 0;
					speedThrowBar = 0;
					stillBombThrow = true;
					
					removeItems();
				}
			}
			
			if(stillBombThrow){
				updateBombAttack(bombIndex);
			}
			
			switch (statePlayer) {
			case Constant.GOTOLEFT:
				goToLeft();
				break;
			case Constant.GOTORIGHT:
				goToRight();
				break;
			case Constant.FISHING:
				updateFishing();
				break;
			case Constant.TIME:
				updateGainTime();
				break;
			case Constant.LINE:
				updateGainLineFishing();
				break;
			case Constant.STRENGTH:
				updateGainStrength();
				break;
			case Constant.HEALTH:
				updateGainHealth();
				break;
//			case Constant.ATTACK:
//				updateBombAttack(bombIndex);
//				break;
//			case Constant.COLLECT:
//				statePlayer = Constant.FISHING;
//				break;
			default:
				break;
			}
			
		}
		
		private void updateGainTime(){
				
			GameScreen.timePercent += functions.gainTime(Assets.userBox.get(GameScreen.itemIndex).getIndex());
			removeItems();
			statePlayer = Constant.STANDBY;
		}
		
		private void updateGainLineFishing(){
			
			lineLengthInLevel += functions.lineFishing(Assets.userBox.get(GameScreen.itemIndex).getIndex());
			removeItems();
			statePlayer = Constant.STANDBY;
		}
		
		private void updateGainStrength(){
			
			GameScreen.strengPercent += functions.gainStrength(Assets.userBox.get(GameScreen.itemIndex).getIndex());
			strength += functions.gainStrength(Assets.userBox.get(GameScreen.itemIndex).getIndex());
			removeItems();
			statePlayer = Constant.STANDBY;
		}
		
		private void updateGainHealth(){
			
			GameScreen.bloodPercent += functions.gainStrength(Assets.userBox.get(GameScreen.itemIndex).getIndex());
			removeItems();
			statePlayer = Constant.STANDBY;
			
		}

		private void updateFishing() {
			if (pushDown) {
				if (lineLength <= lineLengthInLevel) {
					lineLength += lineSpeed;
					// check cau o day
					checkIsCaptureOk();
				} else if (lineLength > lineLengthInLevel) {
					pushDown = false;

				}
			}
			if (!pushDown) {
				if(statePlayer == Constant.CANNOTFISHING){
					lineLength = oldLineLength;
					statePlayer = Constant.STANDBY;
					return; // nếu mắc câu thì return ngay
				}
				
				if (lineLength > oldLineLength && statePlayer != Constant.COLLECT) {
					lineLength -= lineSpeed;
				} 
				if ( lineLength <= oldLineLength) {
					statePlayer = Constant.STANDBY;// phai co trang thai nay de xac
//					iCounter = Constant.BANDAU;							// nhan cau xong
//					Log.d("Locnnt", "co chay vao day ne Constant.STANDBY: "+Constant.STANDBY);
					return;
				}
				
//				System.out.println("LINE LENGTH" + lineLength);
//				System.out.println("OLD LINE: " + oldLineLength);
//				System.out.println("ISCOLLECT: " + isCollect);
//				
//				
				if (lineLength <= oldLineLength + 5 && isCollect) {
//					iCounter = Constant.BATDAU_DEM;
					statePlayer = Constant.COLLECT;// phai co trang thai nay de xac
					isCollect = false;
//					Log.d("Locnnt", "co chay vao day ne Constant.COLLECT: "+Constant.COLLECT);	
					return;// nhan cau xong
				}
			}
		}

		private void checkIsCaptureOk() {
			
			checkCapture(arrFishs);// check voi danh sach ca

		}

		private void checkCapture(Array<Fish> gos) {

			float x_check = x_fishHook + Assets.hookRegion[0].getRegionHeight()/ 2;
			float y_check = y_fishHook;

			for (Fish go : gos) {
				if (Constant.checkCollision((int) x_check, (int) y_check,
						(int) go.position.x, (int) go.position.y,
						(int) go.position.x + (int) go.bounds.getWidth(),
						(int) go.position.y + (int) go.bounds.getHeight())) {
					// xu ly bat giu o day
//					System.out.println("checkCapture ============ ok roi ne ===========");
					go.setState(Constant.FISH_KEOLEN);// danh dau la da cau trung,ca nay se duoc
									// update theo chieu nguoc len
				
					 float tempWeight = go.getWeight()*30; //30 don vi suc khoe thi cau duoc 1kg
//					System.out.println("weight ============  ===========: "+go.getWeight());
//					System.out.println("tempWeight ============  ===========: "+tempWeight);
//					System.out.println("strength ============  ===========: "+strength);
					if((int)tempWeight > strength){
						// cá mắc câu nhưng ko kéo lên được
						pushDown = false;// bat co lai day de day cau ko xuong sau hon duoc nua
						go.setState(Constant.FISH_GIATCAU);
						isCatchOk = true;
						statePlayer  = Constant.CANNOTFISHING;
						isCollect = false;
						return;
					}
					else if((int)tempWeight <= strength){
						// cá mắc câu và bị kéo lên
						pushDown = false;// bat co lai day de day cau ko xuong sau hon duoc nua
						go.y_Speed = lineSpeed;// cá bị kéo lên cùng tốc độ dây
						go.setState(Constant.FISH_KEOLEN);
						isCatchOk = true;
						isCollect = true;
						go.setMoneyX(xMoney);
						go.setMoneyY(yMoney);
						
						for(int i = 0; i < fishLevel.size(); i++)
						{
							if(go.getKind() == fishLevel.get(i).getKindFishToPass()){
								if(fishLevel.get(i).getFishAmountToPass() > 0){
									fishLevel.get(i).setFishAmountToPass(fishLevel.get(i).getFishAmountToPass() - 1);
									break;
								}
							}
						}
					}
					return;
				}
			}
		}
		
		public void checkBoatCollisionFishDeadByBomb(){
			
			for(Fish go : arrFishs){
				if(go.getState() == Constant.FISH_DEAD){
//					System.out.println("================================");
//					System.out.println("BOAT X: " + (xCollectFish0) + "- BOAT Y: " + (yCollectFish));
//					System.out.println("FISH X: " + (int)go.position.x + "- FISH Y: " + (int)go.position.y);
//					System.out.println("FISH EXTRA X: " + ((int)go.position.x + (int) go.bounds.getWidth()));
//					System.out.println("FISH EXTRA Y: " + ((int)go.position.y + (int) go.bounds.getHeight()));
//					System.out.println("================================");
					if (Constant.checkCollisionFishCollect((int) (xCollectFish0),(int) (xCollectFish1), (int) (yCollectFish),
							(int) go.position.x, (int) go.position.y,
							(int) go.position.x + (int) go.bounds.getWidth(),
							(int) go.position.y + (int) go.bounds.getHeight())) {
						go.setEarnBombMoney(true);
						go.setMoneyX(xMoney);
						go.setMoneyY(yMoney);
						
						statePlayer = Constant.COLLECT;
					}
				}
			}
		}

		private void addBomb(float speedThrowBomb, int index, int id) {
//			System.out.println("DIRECTION: " + direction);
				
				Bomb bomb = new Bomb(0, (int)x_throwBomb, (int)y_throwBomb, 0.01f, speedThrowBomb, direction, index, id);
				arrBombs.add(bomb);
				return;
		}
		
		private void updateBombAttack(int index)
		{	
			
			for(int i = 0; i < arrBombs.size; i++)
			{
				Bomb bomb = arrBombs.get(i);
				if(checkBombOutOfBounds(bomb, i)){
					i--;
					continue;
				}
				
				boolean highLevel = false;
				
				for(int j = 0; j < arrFishs.size; j++) {
					
					Fish fish = arrFishs.get(j);
					
					if(!overFlowFire){
						
						overFlowFireBombX = (int) bomb.bounds.getWidth();
						overFlowFireBombY = (int) bomb.bounds.getHeight();
						
					}else{		
						
						overFlowFireBombX = bomb.getFireCenterWidth();
						overFlowFireBombY = bomb.getFireCenterHeight();
						
						countFireBomb++;
//						bomb.setBombRange(functions.bombRange(index));
						
							for(Fish f : tempArrFish)
							{
								for(int k = 0; k < fishLevel.size(); k++)
								{
									if(f.getKind() == fishLevel.get(k).getKindFishToPass()){
										if(fishLevel.get(k).getFishAmountToPass() > 0){
											fishLevel.get(k).setFishAmountToPass(fishLevel.get(k).getFishAmountToPass() - 1);
											break;
										}
									}
										
								}
//								if(f.kind == kindFishToPassLevel){
//									if(numFishToPassLevel>0) numFishToPassLevel--;
//								}
								
								tempArrFish = new Array<Fish>();
							}
							
							if(bomb.isFinished())
							{	
								Colliders.removeOutOfBombBounds(bombPool, arrBombs, bomb, i);
								bomb.setBombTime(3);
								bomb.setFired(false);
								overFlowFire = false;
								countFireBomb = 0;
								tempIndexFish = new Array<Integer>();
								highLevel =false;
								
								fastDown = 0;
								strengthDown = 0;
								
								if(arrBombs.size == 0){
									stillBombThrow = false;
								}
								
							}			
					}
					
					if ((Constant.checkCollision((int) bomb.position.x, (int) bomb.position.y,
							overFlowFireBombX, overFlowFireBombY,
							(int) fish.position.x, (int) fish.position.y,
							(int) fish.bounds.getWidth(),
							(int) fish.bounds.getHeight()) && index < 3) ||
							
						(Constant.checkCollision((int) bomb.position.x, (int) bomb.position.y,
							overFlowFireBombX, overFlowFireBombY,
							(int) fish.position.x, (int) fish.position.y,
							(int) fish.bounds.getWidth(),
							(int) fish.bounds.getHeight()) && highLevel)) {
								
						if(fish.getState() == Constant.FISH_DANGBOI )
						{
							bomb.setFired(true);					
							fish.setState(Constant.FISH_DEAD);
							fish.y_Speed = 5;
							
							if(tempIndexFish.size == 0)
							{
								tempIndexFish.add(j);
								tempArrFish.add(fish);
							}
							
							for(int k = 0; k < tempIndexFish.size; k++)
							{
								if(tempIndexFish.get(k) == j)
								{
									checkFish = true;
									break;
								}
								else if(tempIndexFish.get(k) != j)
								{
									checkFish = false;
								}
							}
							
							if(!checkFish)
							{
								tempIndexFish.add(j);
								tempArrFish.add(fish);
							}
				
							overFlowFire = true;
						}	
					}
					else if(bomb.getBombTime() < 0 && index >=3){
						
						bomb.setFired(true);
						overFlowFire = true;
						highLevel = true;
					}
					else if(bomb.position.y == 2){
						bomb.setFired(true);
						overFlowFire = false;  
					}
					
				}		
			}
		}

		private void goToRight() {
			if (this.position.x <= (Constant.SCREEN_WIDTH * 6)
					- Assets.staticboatGoLeft[0].getRegionWidth()) {
				
				if(camera.position.x != 4400 && this.position.x >= 300)
					camera.position.add(2, 0, 0);	
				
				
				this.position.add(speed, 0);	
				
			} else {
				statePlayer = Constant.STANDBY;
			}
		}

		private void goToLeft() {
//			System.out.println("goToLeft:   ============ ==========="+worldState);
			if (0 <= this.position.x) {
				
				if(camera.position.x != 400 && this.position.x <= 4300)	
					camera.position.add(-2, 0, 0);	
				
				this.position.sub(speed, 0);
			
			} else {
				statePlayer = Constant.STANDBY;
			}
		}

		public void setState(int state) {
			this.statePlayer = state;
		}
		
		public void removeItems(){
			
			if(Assets.userBox.get(GameScreen.itemIndex).getQuantity() > 1){
				Assets.userBox.get(GameScreen.itemIndex).setQuantity(Assets.userBox.get(GameScreen.itemIndex).getQuantity() - 1);
			}else{
				Assets.userBox.remove(GameScreen.itemIndex);
				GameScreen.itemIndex = -1;
			}
		}
		
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getLevel() {
		return this.level;
	}
	public void setFishToPassLevel(int kind) {
		this.kindFishToPassLevel = kind;
	}

	public int getFishToPassLevel() {
		return this.kindFishToPassLevel;
	}
	public void setnumFishToPassLevel(int num) {
		this.numFishToPassLevel = num;
	}

	public int getnumFishToPassLevel() {
		return this.numFishToPassLevel;
	}

	public void setStrengthDown(int strengthDown) {
		this.strengthDown = strengthDown;
	}

	public int getStrengthDown() {
		return strengthDown;
	}

	public void setFishLevel(List<FishAmountLevel> fishLevel) {
		this.fishLevel = fishLevel;
	}

	public List<FishAmountLevel> getFishLevel() {
		return fishLevel;
	}
	public int getAmoutMoney() {
		return player.amountOfMoney;
	}
	public int getPlayerStreng() {
		return player.strength;
	}
	public int getPlayerBlood() {
		return player.blood;
	}
}
